//
//  ShareContrller.m
//  ShareDemo
//
//  Created by kai ren on 26/09/2011.
//  Copyright 2011 none. All rights reserved.
//

#import "ShareContrller.h"


@implementation ShareContrller

@synthesize content, superCtrl;

- (id)initWithContent:(NSString *)_content superCtrl:(UIViewController*)_superCtrl{
	if (self = [super initWithTitle:nil
						   delegate:self
				  cancelButtonTitle:NSLocalizedString(@"Cancel", nil)
			 destructiveButtonTitle:nil
				  otherButtonTitles:NSLocalizedString(@"Sina weibo", nil),
									NSLocalizedString(@"Tencent weibo", nil),
									NSLocalizedString(@"SMS", nil), 
									NSLocalizedString(@"E-mail", nil), nil]
		) 
	{
		self.content = _content;
		self.superCtrl = _superCtrl;
	}
	
	return self;
}

#pragma mark -
#pragma mark Action

- (void)SinaShare{
	SinaWeiboController *ctrl = [[SinaWeiboController alloc] initWithContent:content];

	[self.superCtrl.navigationController pushViewController:ctrl animated:YES];
	[ctrl release];
}

- (void)TencentShare{
	TencentWeiboController *ctrl = [[TencentWeiboController alloc] initWithContent:content];
	
	[self.superCtrl.navigationController pushViewController:ctrl animated:YES];
	[ctrl release];
}

- (void)SMSShare{
	BOOL canSendSMS = [MFMessageComposeViewController canSendText];
	if (canSendSMS) {
		
		MFMessageComposeViewController *SMSCtrl = [[MFMessageComposeViewController alloc] init];
		SMSCtrl.messageComposeDelegate = self;
		SMSCtrl.navigationBar.tintColor = [UIColor orangeColor];
		SMSCtrl.body = content;
		
		[self.superCtrl presentModalViewController:SMSCtrl animated:YES];
		[self retain];
		[SMSCtrl release];	
	}
}

- (void)MailShare{
	BOOL canSendEmail = [MFMailComposeViewController canSendMail];
	if (canSendEmail) {
		MFMailComposeViewController *EmailCtrl = [[MFMailComposeViewController alloc] init];
		EmailCtrl.mailComposeDelegate = self;
		EmailCtrl.navigationBar.tintColor = [UIColor orangeColor];
		[EmailCtrl setSubject:NSLocalizedString(@"Campus Speech(from 51job)", nil)];
		[EmailCtrl setMessageBody:content isHTML:NO];
		
		[self.superCtrl presentModalViewController:EmailCtrl animated:YES];
		
		[self retain];
		[EmailCtrl release];
	}
}

#pragma mark -
#pragma mark delegate

- (void)alert:(NSString *)msg{ 
	UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil 
													message:msg
												   delegate:self
										  cancelButtonTitle:@"OK"
										  otherButtonTitles:nil];
	[alert show];
	[alert release];
	[self release];
}

- (void) messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result{
	switch (result) {
		case MessageComposeResultSent:
			[self alert:@"Sent"];
			break;
		case MessageComposeResultFailed:
			[self alert:@"Failed"];
			break;
	}
	
	[controller dismissModalViewControllerAnimated:YES];
}

- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
	if (error) {
		[self alert:[error localizedDescription]];
	} else {
		switch (result) {
			case MFMailComposeResultSent:
				[self alert:@"Sent"];
				break;
			case MFMailComposeResultSaved:
				[self alert:@"Saved"];
				break;
			case MFMailComposeResultFailed:
				[self alert:@"Failed"];
				break;
		}
		
		[controller dismissModalViewControllerAnimated:YES];
	}
	
}

#pragma mark -
#pragma mark UIActionSheetDelegate

- (void) actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{
	switch (buttonIndex) {
		case 0:
			[self SinaShare];
			break;
		case 1:
			[self TencentShare];
			
			break;
		case 2:
			[self SMSShare];
			
			break;
		case 3:
			[self MailShare];
			
			break;
	}
}

@end
